package com.aaa.stock.controller.api;


import com.aaa.entity.Medicines;
import com.aaa.entity.Provider;
import com.aaa.stock.service.ProviderService;
import com.aaa.stock.vo.ProviderVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("stock/api/provider")
public class ApiProviderController {
    @Autowired
    private ProviderService providerService;

    //分页查询
    @PostMapping("provpage/{currentPage}/{pageSize}")
    public Result<IPage<Provider>> provpage(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody ProviderVo providerVo
    ){
        return  providerService.findByPage(currentPage,pageSize,providerVo);
    }


    //查询
    @GetMapping("getAll")
    public Result<List<Provider>> getAll(){
        return  providerService.getAll();
    }


    //添加
    @PostMapping("addStu")
    public Result addstu(@RequestBody Provider provider) {
        if (Objects.nonNull(provider.getProviderId())){
            provider.setUpdateTime(LocalDateTime.now());
        }else {
            provider.setCreateTime(LocalDateTime.now());
        }
        providerService.saveOrUpdate(provider);
        return new Result<>(200,"成功");
    }



    //删除
    @DeleteMapping("/{providerId}")
    public Result delStu(@PathVariable Integer providerId){
        providerService.removeById(providerId);
        return new Result<>(200,"删除成功");
    }

    @PostMapping("/del1")
    public Result delStu1(@RequestBody List<Provider> providers){
        providers.forEach((Provider ppp)->{
            boolean b = providerService.removeById(ppp.getProviderId());
        });
        return new Result(200,"成功");
    }
}
